/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.grapeshot.halfnes;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sebastien Dion & Yome Messena Jean
 */
public class Client implements CommunicationInterface{
   final String UTF8 = "UTF8";
    protected static boolean alreadyLoaded = false;
    public boolean isConnected = false;
    public int receivedByte;

    protected static Socket socket;

    protected static BufferedWriter writer;
    protected static BufferedReader reader;

    InputStream inputStream;
    InputStreamReader InputStreamReader;
    BufferedReader bufferedReader ; 
    
    protected static byte[] utfString;
    protected static int numberOfRepetitions = 1;  
    
    protected String serverIp;
    

    @Override
    public void connect(final String ip) {
        
            Runnable clientTask = new Runnable() {
            @Override
            public void run() {
                try {
                                                            
                    System.out.println("Trying to connect to ip : " + ip);
                    
                    //socket.connect(new InetSocketAddress(ip, port), 1000);
                    socket = new Socket(ip, port);
                    isConnected = true;
                    
                    System.out.println("Client is connected to server " + ip);
                    //startListening();
                } catch (UnknownHostException ex) {
                      
                    System.out.println("ERROR on Ip: " + ip);
                    isConnected = false;
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    
                    System.out.println("ERROR on Ip: " + ip);
                    isConnected = false;
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        if (isConnected)
            this.serverIp = ip;
        
        Thread serverThread = new Thread(clientTask);
        serverThread.start();

    } 
    @Override
    public String readLineServer(){
        try {
            String message = ""; 
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            message = bufferedReader.readLine();             
            System.out.println("Server received : " + message);
            return message;
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    public void readServer(){
        try {

            String message = "";            
            int receivedInt = 0;
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            receivedInt = bufferedReader.read();            
            receivedByte = receivedInt;
            
            System.out.println("Server received : " + receivedByte);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        
    }
    
        
    public int getReceivedByte() {
        readServer();
        return receivedByte;
    }
    public void sendData(int byteToSend) {
                try {
                    writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    
                    if(byteToSend==9999){
                        writer.write(readyMessage);
                    }else{
                        writer.write(byteToSend);  
                    }
                        
                    
                    writer.flush();
                    socket.getOutputStream().flush();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    //FIX THIS TRY-CATCH
    public void close(){
        try {
            utils.close(InputStreamReader);
            utils.close(inputStream);
            utils.close(socket);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    @Override
    public void connect() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getDistantIp() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean getConnectionStatus() {
         return isConnected;
    }

    @Override
    public void startListening() throws IOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}
